package cwy.flylab.http;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;

public class HttpClient {

	private String mUrl;

	private String mUserAgent;

	private long mOverTime;
	
	private URLConnection mConnection;
	
	private Proxy mProxy;
	
	private HashMap<String, String> mRequestPropertys = new HashMap<String, String>();

	public HttpClient(String url, String userAgent) {
		mUrl = url;
		mUserAgent = userAgent;
	}
	
	public void setProxy(Proxy proxy) {
		mProxy = proxy;
	}
	
	public HttpClient setUserAgent(String userAgent) {
		mRequestPropertys.put("User-Agent", userAgent);
		return this;
	}
	
	public boolean connect() {
		try {
			URL url = new URL(mUrl);
			URLConnection conn = null;
			if(mProxy != null){
				conn = url.openConnection(mProxy);
			}else {
				conn = url.openConnection();
			}
			if(mRequestPropertys.size() > 0){
				for (Iterator<String> iterator = mRequestPropertys.keySet().iterator(); iterator
						.hasNext();) {
					String key = iterator.next();
					String val = mRequestPropertys.get(key);
					conn.setRequestProperty(key, val);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public void closeConn() {
		
	}
}
